package install

import (
	"fmt"
	"os"
	"os/exec"
)

func install() {
	_ = os.Remove("install-db.sh")
	f, err := os.Create("install-db.sh")
	if err != nil {
		panic(err)
	}
	_, err = f.Write([]byte(installScriptTemp))
	if err != nil {
		panic(err)
	}

	cmd := exec.Command("/bin/bash", "-c", "grep test")

	if err := cmd.Start(); err != nil {
		fmt.Println("Execute failed when Start:" + err.Error())
		return
	}

	if err := cmd.Wait(); err != nil {
		fmt.Println("Execute failed when Wait:" + err.Error())
		return
	}

}

const installScriptTemp = `
#!/bin/bash

yum install git -y

git clone https://gitee.com/xianwl/openGauss-server_2.git
#git clone https://github.com/opengauss-mirror/openGauss-server.git
wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/5.0.0/binarylibs/openGauss-third_party_binarylibs_Centos7.6_x86_64.tar.gz


# fa14e53f89ae598c4a65cca568394bdb  /opt/openGauss-third_party_binarylibs_Centos7.6_x86_64.tar.gz
# # TODO: check sum == fa14e53f89ae598c4a65cca568394bdb
md5sum /opt/openGauss-third_party_binarylibs_Centos7.6_x86_64.tar.gz

tar -zxvf openGauss-third_party_binarylibs_Centos7.6_x86_64.tar.gz
mv openGauss-third_party_binarylibs_Centos7.6_x86_64 binarylibs


yum install libaio-devel flex bison ncurses-devel glibc-devel patch lsb_release readline-devel -y

## and env
export CODE_BASE=/root/openGauss-server
export BINARYLIBS=/root/binarylibs
export GAUSSHOME=$CODE_BASE/mppdb_temp_install
export GCC_PATH=$BINARYLIBS/buildtools/gcc7.3
export CC=$GCC_PATH/gcc/bin/gcc
export CXX=$GCC_PATH/gcc/bin/g++
export LD_LIBRARY_PATH=$GAUSSHOME/lib:$GCC_PATH/gcc/lib64:$GCC_PATH/isl/lib:$GCC_PATH/mpc/lib/:$GCC_PATH/mpfr/lib/:$GCC_PATH/gmp/lib/:$LD_LIBRARY_PATH
export PATH=$GAUSSHOME/bin:$GCC_PATH/gcc/bin:$PATH

#
#./configure --gcc-version=7.3.0 CC=g++ CFLAGS="-O2 -g3" --prefix=$GAUSSHOME --3rd=$BINARYLIBS --enable-thread-safety --with-readline --without-zlib

cd openGauss-server
git checkout 5.0.0
`
